#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QPainter>
#include <QPen>
#include <QBrush>
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

}

Widget::~Widget()
{
    delete ui;
}
void Widget:: paintEvent(QPaintEvent *event){
    QPainter painter(this);//画家
    QPen pen(QColor(Qt::black));//画笔
    QBrush brush(QColor(Qt::white));//画刷
    painter.setPen(pen);//画家拿笔
    painter.setBrush(brush);//画家拿画刷
    painter.drawRect(0,0,this->width()*2/3,this->height());//2/3的矩形
    if(ui->comboBox->currentText() == "line")//画线
        painter.drawLine(start,end);
    else//画矩形
        painter.drawRect(QRect(start,end));
}
void Widget:: mousePressEvent(QMouseEvent *event){
    start = event->pos();//获取起始位置
}
void Widget:: mouseMoveEvent(QMouseEvent *event){
    end =event->pos();
    update();//强制产生一个paint 事件
}
